//
//  WelcomeViewController.swift
//  sinaWeibo
//
//  Created by 张国梁 on 16/4/11.
//  Copyright © 2016年 CoderGL. All rights reserved.
//

import UIKit
import SDWebImage


class WelcomeViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(bgView)
        view.addSubview(iconView)
        view.addSubview(label)
        
        if let iconUrl = UserAccount.getAccountInfo()?.avatar_large {
            iconView.sd_setImageWithURL(NSURL(string: iconUrl))
        }
        
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        bgView.xmg_Fill(view)
        
        iconView.center = view.center
        iconView.frame.origin.y = 140.0
        
        label.frame.origin.y = CGRectGetMaxY(iconView.frame) + 10
        label.center.x = iconView.center.x
    }
    
    
    override func viewDidAppear(animated: Bool) {
         super.viewDidAppear(animated)
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: UIViewAnimationOptions.CurveLinear, animations: {
            
            self.iconView.frame.origin.y = 100.0
            self.iconView.alpha = 1
            self.label.frame.origin.y = CGRectGetMaxY(self.iconView.frame) + 10
            }) { (finished) in
                UIView.animateWithDuration(1.0, animations: {
                    self.label.alpha = 1
                    }, completion: { (_) in
                    NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewControllerKey, object: true)
                        
                })
        }
    }
    
    private lazy var bgView : UIImageView = {
        let bgView = UIImageView(image: UIImage(named: "ad_background"))
        return bgView
    }()
    
    private lazy var iconView : UIImageView = {
        let iconView = UIImageView()
        iconView.image = UIImage(named: "avatar_default_big")
        iconView.frame.size = CGSizeMake(80, 80)
        iconView.layer.cornerRadius = iconView.bounds.size.width * 0.5
        iconView.layer.masksToBounds = true
        
        iconView.alpha = 0
        return iconView
    }()
    
    private lazy var label : UILabel = {
        let label = UILabel()
        label.text = "欢迎回来"
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(14)
        label.sizeToFit()
        label.alpha = 0.0
        return label
    }()
}
